package com.hm.health.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.hm.health.Activity.ecg.EcgHistoryDetailActivity;
import com.hm.health.R;
import com.hm.health.entity.dao.EcgStatistics;

import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chenshi on 2020/3/9.
 */

public class EcgTestRecordAdapter extends RecyclerView.Adapter<EcgTestRecordAdapter.ViewHolder>{
    private List<EcgStatistics> mEcgStatisticsList;
    private Context mContext;

    public EcgTestRecordAdapter(List<EcgStatistics> ecgStatisticsList, Context context) {
        mEcgStatisticsList = ecgStatisticsList;
        mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_ecg_record_summary, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        EcgStatistics statistics = mEcgStatisticsList.get(position);
        holder.mTvDuration.setText(statistics.getDuration());
        holder.mTvStartTime.setText(statistics.getStartTime());
        String percents = String.format(Locale.ENGLISH,
                "%.2f", mEcgStatisticsList.get(position).getLostPrecents());
        holder.mTvLostPercents.setText(percents);

        holder.mTvHrAverage.setText(String.format("%d BPM", mEcgStatisticsList.get(position).getHrAverage()));

        String[] pathological = mContext.getResources().getStringArray(R.array.pathological);

        int type = mEcgStatisticsList.get(position).getPathologicalInformation();
        if (type < pathological.length) {
            holder.mTvPathologicalInformation.setText(pathological[type]);
        } else {
            holder.mTvPathologicalInformation.setText(pathological[0]);
        }

        holder.mLinearLayout.setOnClickListener(view -> {
            Intent intent = new Intent(mContext, EcgHistoryDetailActivity.class);
            intent.putExtra(EcgHistoryDetailActivity.EXT_ECG_HISTORY_DETAIL_INFO,
                    new Gson().toJson(mEcgStatisticsList.get(position)));
            mContext.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return mEcgStatisticsList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.tv_start_time)
        TextView mTvStartTime;

        @BindView(R.id.tv_duration)
        TextView mTvDuration;

        @BindView(R.id.view_empty)
        View mEmptyView;

        @BindView(R.id.tv_lost_percents)
        TextView mTvLostPercents;

        @BindView(R.id.tv_hr_average)
        TextView mTvHrAverage;

        @BindView(R.id.tv_pathological_information)
        TextView mTvPathologicalInformation;

        @BindView(R.id.tv_top_ll)
        LinearLayout mLinearLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
